#delimit ;
clear all;
set more off;
pause on;

/**************************************************************************************************/
/* ABOUT THIS FILE */
/**************************************************************************************************/
/*
PUROPSE
Analyzes the poster data using the poster as the unit of analysis.
Produces tables for the online supplementary materials.
*/

/**************************************************************************************************/
/* OPEN & MACROS */
/**************************************************************************************************/

use "dta/db_pos.dta";

/* Vars Macros ------------------------------------------------------------------------------------*/
local otreg_opt
	bdec(3) sdec(3) rdec(2) symbol(**, *, ^) label;

local ivars
	muslim_pop modern_grdp_pct reg_leg;

local cvars
  female_can java jakarta;

/* Codes to help get summary data ----------------------------------------------------------------*/
gen unique_pos = 1; // Creates 'yes' value for first observation of a new poster
sort can_id;
by can_id: gen unique_can = 1 if _n==1; // Creates 'yes' for first ob of new candidate
sort region;
by region: gen unique_reg = 1 if _n==1; // Creates 'yes' for first ob of new electoral district
sort party region;
by party region: gen unique_regparty = 1 if _n==1; // Creates 'yes' for first ob of new electoral district per party

/**************************************************************************************************/
/* Table A1. Distribution of posters/candidates/districts by province */
/**************************************************************************************************/

tab prov_no;				    // Party Names
tab prov_no, nolabel;		// Party Numbers

sort prov_no;
putexcel set tables/table_a1, replace;
putexcel
	D1:G1 = ("# Electoral districts in the province with posters"),
	merge;
putexcel
	A2 = ("Province Number")
	B2 = ("# Posters")
	C2 = ("# Candidates")
	D2 = ("Kabupaten/Kota (DPRD)")
	E2 = ("National (DPD-RI)")
	F2 = ("Provincial (DPRD)")
	G2 = ("All"),
	txtwrap;
tab prov_no, nolabel matcell(freq) matrow(names);
putexcel A3=matrix(names);
tab prov_no if unique_pos == 1, matcell(freq);
putexcel B3=matrix(freq);
tab prov_no if unique_can == 1, matcell(freq);
putexcel C3=matrix(freq);
tab prov_no election if unique_reg == 1, matcell(freq);
putexcel D3=matrix(freq);
tab prov_no if unique_reg == 1, matcell(freq);
putexcel G3=matrix(freq);

/**************************************************************************************************/
/* Table A2. Distribution of posters/candidates/districts by party */
/**************************************************************************************************/

tab party;				    // Party Names
tab party, nolabel;		// Party Numbers
tab party is_party; 	// Size of each party

sort party;
putexcel set tables/table_a2, replace;
putexcel
	D1:G1 = ("# Electoral districts with posters from each party"),
	merge;
putexcel
	A2 = ("Party Number")
	B2 = ("# Posters")
	C2 = ("# Candidates")
	D2 = ("Kabupaten/Kota (DPRD)")
	E2 = ("National (DPD-RI)")
	F2 = ("Provincial (DPRD)")
	G2 = ("All"),
	txtwrap;
tab party, nolabel matcell(freq) matrow(names);
putexcel A3=matrix(names);
tab party if unique_pos == 1, matcell(freq);
putexcel B3=matrix(freq);
tab party if unique_can == 1, matcell(freq);
putexcel C3=matrix(freq);
tab party election if unique_regparty == 1, matcell(freq);
putexcel D3=matrix(freq);
tab party if unique_regparty == 1, matcell(freq);
putexcel G3=matrix(freq);

drop unique_can unique_pos unique_reg unique_regparty;


/**************************************************************************************************/
/* Table A12.	Summary statistics for DVs (UOA = poster) */
/**************************************************************************************************/

sum nat_pos islam_pos, separator(0);
estpost summarize nat_pos islam_pos;
esttab using "tables/table_a12.csv",
	cells("count(fmt(0)) mean(fmt(3)) sd(fmt(3)) min(fmt(0)) max(fmt(0))")
	noobs nonumber label title(Summary statistics for dependent variables (UOA = Poster)) replace;
eststo clear;

/**************************************************************************************************/
/* Table A13.	Summary statistics for IVs (UOA = poster) */
/**************************************************************************************************/

sum is_party `ivars' `cvars', separator(0);
estpost summarize is_party `ivars' `cvars';
esttab using "tables/table_a13.csv",
	cells("count(fmt(0)) mean(fmt(3)) sd(fmt(3)) min(fmt(2)) max(fmt(2))")
	noobs nonumber label title(Summary statistics for independent variables (UOA = Poster)) replace;
eststo clear;

/**************************************************************************************************/
/* Table A14.	Correlation matrix for IVs (UOA = poster) */
/**************************************************************************************************/

corr is_party `ivars' `cvars';
estpost corr is_party `ivars' `cvars', matrix;
esttab using "tables/table_a14.csv",
  cells(rho(fmt(3)))
  not unstack compress noobs nonumber label title(Correlation matrix for independent variables (UOA = Poster)) replace;

/**************************************************************************************************/
/* Table A15.	Candidate Poster Campaigns (OLS) (UOA = poster) */
/**************************************************************************************************/

reg nat_pos is_party `ivars' `cvars', robust;
outreg2 using tables/table_a15, `otreg_opt' title(OLS Model UOA = Poster) replace;
reg nat_pos `ivars'  `cvars' if is_party == 0, robust;
outreg2 using tables/table_a15, `otreg_opt';
reg nat_pos `ivars'  `cvars' if is_party == 1, robust;
outreg2 using tables/table_a15, `otreg_opt';
reg islam_pos is_party `ivars' `cvars', robust;
outreg2 using tables/table_a15, `otreg_opt';
reg islam_pos `ivars'  `cvars' if is_party == 0, robust;
outreg2 using tables/table_a15, `otreg_opt';
reg islam_pos `ivars'  `cvars' if is_party == 1, robust;
outreg2 using tables/table_a15, `otreg_opt' excel;
erase tables/table_a15.txt;

/* END *********************************************************************************************/
